package com.olocat.cjx.nsims.service.impl

import com.olocat.cjx.nsims.dao.NurseRepository
import com.olocat.cjx.nsims.dao.SchedulingRepository
import com.olocat.cjx.nsims.pojo.info.Scheduling
import com.olocat.cjx.nsims.pojo.personnel.Nurse
import com.olocat.cjx.nsims.pojo.personnel.Patient
import com.olocat.cjx.nsims.service.NurseService
import org.springframework.stereotype.Service
import javax.annotation.Resource

@Service
class NurseServiceImpl:NurseService {

    @Resource
    lateinit var nurseRepository: NurseRepository
    @Resource
    lateinit var schedulingRepository: SchedulingRepository

    override fun getByName(name: String): List<Nurse> {
        return nurseRepository.getByName(name)
    }

    override fun save(nurse: Nurse) {
        nurseRepository.save(nurse)
        val n:Nurse? = nurse.phone?.let { getByPhone(it) }
        if (n != null) {
            val s = Scheduling()
            s.nurseId = n.id
            schedulingRepository.save(s)
        }
    }

    override fun list(): List<Nurse> {
        return nurseRepository.findAll()
    }

    override fun getById(id: String): Nurse {
        return nurseRepository.getOne(id)
    }

    override fun removeById(id: String) {
        return nurseRepository.deleteById(id)
    }

    override fun updateById(nurse: Nurse) {
        nurseRepository.save(nurse)
    }

    override fun getByPhone(phone: String): Nurse {
        return nurseRepository.getNurseByPhone(phone)
    }

    override fun getNursesByDepartment(department: Int): List<Nurse> {
        return nurseRepository.getNursesByDepartment(department)
    }


}